﻿using System;
using System.IO;

namespace Mars.Network
{
    public sealed class ReceiveState : IDisposable
    {
        public const int DefaultBufferLength = 1024 * 64;
        public const int PacketHeaderLength = 14;
        private bool _disposed;

        public ReceiveState()
        {
            Stream = new MemoryStream(DefaultBufferLength);
            _disposed = false;
        }

        public MemoryStream Stream { get; private set; }

        /// <summary>
        /// 是否为空消息体
        /// </summary>
        public bool IsEmptyBody { get; private set; }

        public IPacketReceiveHeaderHandler PacketHeader { get; set; }

        public void PrepareForPacketHeader(int packetHeaderLength = PacketHeaderLength)
        {
            Reset(packetHeaderLength, null);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                if (Stream != null)
                {
                    Stream.Dispose();
                    Stream = null;
                }
            }

            _disposed = true;
        }

        public void Reset(int targetLength, IPacketReceiveHeaderHandler packetHeader)
        {
            if (targetLength < 0)
            {
                throw new Exception("Target length is invalid.");
            }

            Stream.Position = 0L;
            Stream.SetLength(targetLength);

            if (targetLength == 0)
            {
                // 发现内容长度为空.说明是个空消息或者内容是默认值.
                IsEmptyBody = true;
            }
            else
            {
                IsEmptyBody = false;
            }

            PacketHeader = packetHeader;
        }
    }
}